//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;
using CommunityServer.Discussions.Controls.PostDisplay;

namespace CommunityServer.Discussions.Controls 
{
	public class PostFlatPreview : TemplatedWebControl 
	{
		protected override void OnInit(EventArgs e) 
		{
			if (SkinName == null)                
				ExternalSkinFileName = "View-PostFlatPreview.ascx";
			else 
				ExternalSkinFileName = SkinName;

			base.OnInit(e);
		}

		#region Child Controls

		Repeater postRepeater;

		#endregion

		#region Properties
		public bool LinkToParent
		{
			get
			{
				Object state = ViewState["LinkToParent"];
				if (state != null)
					return (Boolean)state;

				return false;
			}
			set { ViewState["LinkToParent"] = value; }
		}
		#endregion

		#region Skin

		protected override void AttachChildControls() 
		{
			postRepeater = (Repeater)FindControl( "PostRepeater" );

			InitializeChildControls();
		}

		public object DataSource
		{
			get
			{
				EnsureChildControls();
				return postRepeater.DataSource;
			}
			set
			{
				EnsureChildControls();
				postRepeater.DataSource = value;
			}
		}

		private void InitializeChildControls() 
		{
			postRepeater.ItemDataBound += new RepeaterItemEventHandler(postRepeater_ItemDataBound);
		}

		#endregion

		#region DataBinding

		public override void DataBind()
		{
			base.DataBind ();

			postRepeater.DataBind();
        }


		private void postRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e) 
		{
			if( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem ) 
			{
				ForumPost post = e.Item.DataItem as ForumPost;

				User user = post.User;

				switch ( e.Item.ItemType ) 
				{
					case ListItemType.Item:
					case ListItemType.AlternatingItem:

						UserOnlineStatus onlineStatus = (UserOnlineStatus)e.Item.FindControl( "OnlineStatus" );
						UserAvatar avatar = (UserAvatar)e.Item.FindControl( "Avatar" );
						Literal username = (Literal)e.Item.FindControl( "Username" );
						PostIcons postIcon = (PostIcons)e.Item.FindControl( "PostIcon" );
						RoleIcons roleIcon = (RoleIcons)e.Item.FindControl( "RoleIcon" );
						UserAttribute joinedAttribute = (UserAttribute)e.Item.FindControl( "JoinedAttribute" );
						UserAttribute locationAttribute = (UserAttribute)e.Item.FindControl( "LocationAttribute" );
						UserAttribute postsAttribute = (UserAttribute)e.Item.FindControl( "PostsAttribute" );
						UserAttribute pointsAttribute = (UserAttribute)e.Item.FindControl( "PointsAttribute" );
						Literal emoticon = (Literal)e.Item.FindControl( "Emoticon" );
						HyperLink subject = (HyperLink)e.Item.FindControl( "Subject" );
						TextPost textPost = (TextPost)e.Item.FindControl( "TextPost" );
						HtmlAnchor postAnchor = (HtmlAnchor)e.Item.FindControl( "PostAnchor" );
						Literal postPoints = (Literal)e.Item.FindControl( "PostPoints" );

						// Notice the following controls that the user should be seen as anonymous
						//
						bool showUserAsAnonymous = (post.IsAnonymousPost);

						if (onlineStatus != null) 
						{ 
							onlineStatus.User = user;
							onlineStatus.IsCloakedUser = showUserAsAnonymous;
						}

						if (avatar != null) 
						{
							avatar.User = user;
							avatar.IsCloakedUser = showUserAsAnonymous;
						}

						if (username != null)
						{
							if (post.PostShouldBeIgnored)
							{
								username.Text = Formatter.FormatUsername ( user.UserID, post.Username, true, true, true );
							}
							else
							{
								username.Text = Formatter.FormatUsername ( user.UserID, post.Username, showUserAsAnonymous, true, false );
							}
						}

						if (postIcon != null) 
						{
							postIcon.User = user;
							postIcon.IsCloakedUser = showUserAsAnonymous;
						}

						if (roleIcon != null) 
						{
							roleIcon.User = user;
							roleIcon.IsCloakedUser = showUserAsAnonymous;
						}

						if (joinedAttribute != null) 
						{
							joinedAttribute.User = user;
							joinedAttribute.IsCloakedUser = showUserAsAnonymous;
						}

						if (locationAttribute != null) 
						{
							locationAttribute.User = user;
							locationAttribute.IsCloakedUser = showUserAsAnonymous;
						}

						if (postsAttribute != null) 
						{
							postsAttribute.User = user;
							postsAttribute.IsCloakedUser = showUserAsAnonymous;
						}

						if (pointsAttribute != null)
						{
							pointsAttribute.User = user;
							pointsAttribute.IsCloakedUser = showUserAsAnonymous;
						}

						if (emoticon != null)
							emoticon.Text = Formatter.GetEmotionMarkup( post.EmoticonID );

						if (subject != null)
							subject.Text = post.Subject;

						if ((subject != null) && (LinkToParent) && (post.PostLevel != 1))
							subject.NavigateUrl = Globals.GetSiteUrls().Post(post.ParentID);

						if (textPost != null)
						{
							textPost.Post = post;
							textPost.DataBind();
						}

						if (postAnchor != null)
							postAnchor.Name = post.PostID.ToString();

						if (postPoints != null)
							postPoints.Text = Formatter.PostPoints( post );

						break;
				}
			}
		}

		#endregion

	}
}
